package com.yksj.healthtalk.ui.salon;


import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.InfoAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.GroupInfoEntity;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.views.PullToRefreshListView;

/**
* @ClassName: SalonSearchMainActivity 
* @Description: 搜索界面主界面
* @author wt
* @date 2013-1-18 上午10:24:12
 */
public class SalonSearchMainActivity extends BaseFragmentActivity implements OnClickListener,OnItemClickListener{
	
	private ListView mListView;
	private PullToRefreshListView refreshListView;
	private String[] datas;
	private String [] dataIds;
	private ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
	private InfoAdapter adapter;
	private EditText mSalonName;
	private String[][] arrs;
	private String[][] arrsCodes;
	private GroupInfoEntity cacheEntity;
	private int upperPosition;//大类列表的position
	private String [] InfoLayNameArrs;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.salon_search_main_layout);
		initWidget();
		initData();
	}
	
	
	/**
	* @Title: initData
	* @Description: 
	* @param  初始化数据
	* @return void    
	* @throws
	 */
	private void initData() {
		refreshListView.setLayoutInvisible();
		mListView = refreshListView.getRefreshableView();
		
		String[] atts = { "全部","求医","问药","疾病","情感","两性","养生","美食","运动","娱乐","生活点滴","美容美妆","心理","女人","摄影","绘画","收藏","时尚","旅游","社会","职场","居住","婚庆","购物","汽车"};
		 dataIds = new String[]{"0","10000001","10000002","10000003","10000004","10000005","10000006","10000007","10000008","10000009","10000010","10000011","10000012","10000013","10000014","10000015","10000016","10000017","10000018","10000019","10000020","10000021","10000022","10000023","10000024"};
			datas = atts;
			setData(datas);
		adapter = new InfoAdapter(this, data, true);
		adapter.seticonImagView(true);
		adapter.seticonImagViewExceptTheFirst(true);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);
		
		if (arrs == null) {
			queryPoPData(this);
		}
		
		//初始化数据
		if (getIntent().hasExtra("mGroupInfoEntity")) {
			cacheEntity = (GroupInfoEntity) getIntent().getExtras().get("mGroupInfoEntity");
			if (cacheEntity == null) {
				cacheEntity = new GroupInfoEntity();
			}
			
			//姓名
			if (!TextUtils.isEmpty(cacheEntity.getName())) {
				mSalonName.setText(cacheEntity.getName());
			}
			
			//选项 大类
			if (!TextUtils.isEmpty(cacheEntity.getUpperId()) && !cacheEntity.getUpperId().equals("0")) {
					for (int i = 0; i < dataIds.length; i++) {
						if (dataIds[i].equals(cacheEntity.getUpperId())) {
							upperPosition = i;
						}
					}
			}
			
			
			//选项小类
			if (hasValue(cacheEntity.getInfoId())&&upperPosition != 0) {
				adapter.setInfoLayNameVisible(true);
				adapter.setInfoLayName(upperPosition, cacheEntity.getInfoLayName());
				adapter.notifyDataSetChanged();
			}
		}
		
	}
	
	private void setData(String[] strs) {
		// TODO Auto-generated method stub
		for (int i = 0; i < strs.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("id", i);
			map.put("title", strs[i]);
			map.put("content", "");
			data.add(map);
		}
	}
	
	/**
	* @Title: initWidget
	* @Description:控件 
	* @param    
	* @return void    
	* @throws
	 */
	private void initWidget() {
		initTitle();
		 refreshListView = (PullToRefreshListView)findViewById(R.id.att_list);
		 mSalonName = (EditText) findViewById(R.id.name_et);
		
		//设置监听
		titleLeftBtn.setOnClickListener(this);
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText(R.string.sure);
		titleTextV.setText(R.string.salon_level);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.title_right2:
				//判断是否输入了名称
			Intent intent  = new Intent(this,SalonCollectionMainFragment.class);
			intent.putExtra("salonName",mSalonName.getText().toString().trim() );
			cacheEntity.setName(mSalonName.getText().toString().trim());
			intent.putExtra("mGroupInfoEntity", cacheEntity);
				if (adapter.isInfoLayNameVisible() == false) {
					intent.putExtra("dataClickId", "0");
					intent.putExtra("positions", new ArrayList<String>());
				}else {
					intent.putExtra("dataClickId", cacheEntity.getUpperId());
					ArrayList<String> list = new ArrayList<String>();
					list.add(cacheEntity.getInfoId());
					intent.putExtra("positions",list);
				}
				setResult(0, intent);
				finish();			
			break;
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(data==null)return;
		switch (requestCode) {
		case 0:
				Intent intent = new Intent(this, SalonCollectionMainFragment.class);
				ArrayList<String> selects = (ArrayList<String>) data.getExtras().get("positions");
				String dataClickId = data.getExtras().getString("dataClickId");
				intent.putExtra("positions", selects);
				intent.putExtra("dataClickId",dataClickId);
				intent.putExtra("mGroupInfoEntity", cacheEntity);
				//小类的列表的position
				int position = (Integer) data.getExtras().get("position");
				//设置小类name
				cacheEntity.setInfoLayName(InfoLayNameArrs[position]);
				setResult(0, intent);
				finish();
			break;
		}
		
	}
	
	/**
	 * 查询数据
	 * @param context
	 */
	private void queryPoPData(Context context) {
		String [] noLimit ={"全部"};
		String[] seedoctorlevel = context.getResources().getStringArray(R.array.seedoctorlevel);
		String[] seedoctorlevelID = context.getResources().getStringArray(R.array.seedoctorlevelID);
		String[] medicinelevel = context.getResources().getStringArray(R.array.medicinelevel);
		String[] medicinelevelID = context.getResources().getStringArray(R.array.medicinelevelID);
		String[] diseaselevel = context.getResources().getStringArray(R.array.diseaselevel);
		String[] diseaselevelID = context.getResources().getStringArray(R.array.diseaselevelID);
		String[] emotionlevel = context.getResources().getStringArray(R.array.emotionlevel);
		String[] emotionlevelID = context.getResources().getStringArray(R.array.emotionlevelID);
		String[] amphigonylevel = context.getResources().getStringArray(R.array.amphigonylevel);
		String[] amphigonylevelID = context.getResources().getStringArray(R.array.amphigonylevelID);
		String[] healthcarelevel = context.getResources().getStringArray(R.array.healthcarelevel);
		String[] healthcarelevelID = context.getResources().getStringArray(R.array.healthcarelevelID);
		String[] cookinglevel = context.getResources().getStringArray(R.array.cookinglevel);
		String[] cookinglevelID = context.getResources().getStringArray(R.array.cookinglevelID);
		String[] sportlevel = context.getResources().getStringArray(R.array.sportlevel);
		String[] sportlevelID = context.getResources().getStringArray(R.array.sportlevelID);
		String[] entertainmentlevel = context.getResources().getStringArray(R.array.entertainmentlevel);
		String[] entertainmentlevelID = context.getResources().getStringArray(R.array.entertainmentlevelID);
		String[] lifestylelevel = context.getResources().getStringArray(R.array.lifestylelevel);
		String[] lifestylelevelID = context.getResources().getStringArray(R.array.lifestylelevelID);
		String[] cosmeticlevel = context.getResources().getStringArray(R.array.cosmeticlevel);
		String[] cosmeticlevelID = context.getResources().getStringArray(R.array.cosmeticlevelID);
		String[] psychologylevel = context.getResources().getStringArray(R.array.psychologylevel);
		String[] psychologylevelID = context.getResources().getStringArray(R.array.psychologylevelID);
		String[] womanlevel = context.getResources().getStringArray(R.array.womanlevel);
		String[] womanlevelID = context.getResources().getStringArray(R.array.womanlevelID);
		String[] photographylevel = context.getResources().getStringArray(R.array.photographylevel);
		String[] photographylevelID = context.getResources().getStringArray(R.array.photographylevelID);
		String[] drawinglevel = context.getResources().getStringArray(R.array.drawinglevel);
		String[] drawinglevelID = context.getResources().getStringArray(R.array.drawinglevelID);
		String[] collectlevel = context.getResources().getStringArray(R.array.collectlevel);
		String[] collectlevelID = context.getResources().getStringArray(R.array.collectlevelID);
		String[] fashionlevel = context.getResources().getStringArray(R.array.fashionlevel);
		String[] fashionlevelID = context.getResources().getStringArray(R.array.fashionlevelID);
		String[] travellevel = context.getResources().getStringArray(R.array.travellevel);
		String[] travellevelID = context.getResources().getStringArray(R.array.travellevelID);
		String[] societylevel = context.getResources().getStringArray(R.array.societylevel);
		String[] societylevelID = context.getResources().getStringArray(R.array.societylevelID);
		String[] professionallevel = context.getResources().getStringArray(R.array.professionallevel);
		String[] professionallevelID = context.getResources().getStringArray(R.array.professionallevelID);
		String[] livelevel = context.getResources().getStringArray(R.array.livelevel);
		String[] livelevelID = context.getResources().getStringArray(R.array.livelevelID);
		String[] weddinglevel = context.getResources().getStringArray(R.array.weddinglevel);
		String[] weddinglevelID = context.getResources().getStringArray(R.array.weddinglevelID);
		String[] shoppinglevel = context.getResources().getStringArray(R.array.shoppinglevel);
		String[] liveleveshoppinglevelIDlID = context.getResources().getStringArray(R.array.shoppinglevelID);
		String[] carlevel = context.getResources().getStringArray(R.array.carlevel);
		String[] carlevelID = context.getResources().getStringArray(R.array.carlevelID);
	
		arrs = new String[][] { noLimit,seedoctorlevel, medicinelevel, diseaselevel,
				emotionlevel, amphigonylevel, healthcarelevel, cookinglevel,
				sportlevel, entertainmentlevel, lifestylelevel, cosmeticlevel,
				psychologylevel, womanlevel, photographylevel,drawinglevel,collectlevel,fashionlevel,travellevel,societylevel,professionallevel,livelevel,weddinglevel,shoppinglevel,carlevel };
		arrsCodes = new String[][] { {"0"},seedoctorlevelID, medicinelevelID, diseaselevelID,
				emotionlevelID, amphigonylevelID, healthcarelevelID, cookinglevelID,
				sportlevelID, entertainmentlevelID, lifestylelevelID, cosmeticlevelID,
				psychologylevelID, womanlevelID, photographylevelID,drawinglevelID,collectlevelID,fashionlevelID,travellevelID,societylevelID,professionallevelID,livelevelID,weddinglevelID,liveleveshoppinglevelIDlID,carlevelID };
	}
	

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if (position !=0) {
			Intent intent = new Intent();
			intent.setClass(this, SalonSearchSecondaryMenuActivity.class);
			InfoLayNameArrs = arrs[position];
			Bundle bundle = new Bundle();
			bundle.putStringArray("data", arrs[position]);
			bundle.putStringArray("dataCode", arrsCodes[position]);
			bundle.putString("whichDataClick", datas[position]);
			bundle.putString("dataClickId", dataIds[position]);
			bundle.putParcelable("mGroupInfoEntity",cacheEntity);
			intent.putExtras(bundle);
			startActivityForResult(intent, 0);
		}else {
			adapter.seticonImagViewExceptTheFirst(true);
			adapter.setInfoLayNameVisible(false);
			mListView.setAdapter(adapter);
		}
	}
	
	/**
	 * 
	 * @param str
	 * @return
	 */
	private boolean hasValue(String str){
		if (str != null && !str.equals("")) {
			return true;
		}
		return false;
	}

}
